<template>
  <div class="wrapper">
    <img
      class="wrapper__img"
      src="http://www.dell-lee.com/imgs/vue3/user.png"
    />
    <div class="wrapper__input">
      <input
        type="text"
        class="wrapper__input__content"
        placeholder="请输入用户名"
        v-model="username"
      />
    </div>
    <div class="wrapper__input">
      <input
        type="password"
        class="wrapper__input__content"
        placeholder="请输入密码"
        v-model="password"
      />
    </div>
    <div class="wrapper__input">
      <input
        type="password"
        class="wrapper__input__content"
        placeholder="请确认密码"
        v-model="confirmPwd"
      />
    </div>
    <div class="wrapper__register-button" @click="handleRegister">注册</div>
    <div class="wrapper__register-link" @click="handleLoginClick">
      已有账号去登录
    </div>
  </div>
  <Toast v-if="show" :message="toastMessage" />
</template>

<script>
import { reactive, toRefs } from 'vue';
import { useRouter } from 'vue-router';
import { post } from '../../utils/request.js';
import Toast, { useToastEffect } from '../../components/Toast.vue';

// 登录处理
const userRegisterEffect = (showToast) => {
  const router = useRouter();
  const data = reactive({
    username: '18900000000',
    password: '123456',
    confirmPwd: '123456',
  });

  // 注册请求处理
  const handleRegister = async () => {
    if (data.username === '' || data.password === '' || data.confirmPwd === '')
      return showToast('账号密码不能为空');
    if (data.password.trim() === data.confirmPwd.trim()) {
      try {
        const result = await post('/api/user/register', {
          username: data.username,
          password: data.password,
        });
        // console.log(result.data);
        if (result?.errno === 0) {
          router.push({ name: 'Login' });
        } else {
          showToast('注册失败');
        }
      } catch (error) {
        showToast('请求失败');
        // console.log(error);
      }
    } else {
      showToast('密码不一致');
    }
  };
  const { username, password, confirmPwd } = toRefs(data);

  return { username, password, confirmPwd, handleRegister };
};
// 处理登录
const userLoginEffect = () => {
  const router = useRouter();
  // 跳转登录
  const handleLoginClick = () => {
    router.push({ name: 'Login' });
  };

  return { handleLoginClick };
};

export default {
  name: 'Register',
  components: { Toast },
  setup() {
    const { show, toastMessage, showToast } = useToastEffect();
    const { username, password, confirmPwd, handleRegister } =
      userRegisterEffect(showToast);
    const { handleLoginClick } = userLoginEffect();

    return {
      username,
      password,
      confirmPwd,
      show,
      toastMessage,
      handleRegister,
      handleLoginClick,
    };
  },
};
</script>

<style lang="scss" scoped>
@import '../../style/variables.scss';
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  &__img {
    display: block;
    margin: 0 auto 0.4rem auto;
    width: 0.66rem;
    height: 0.66rem;
  }
  &__input {
    height: 0.48rem;
    margin: 0 0.4rem 0.16rem;
    border-radius: 6px;
    border: 1px solid rgba($color: #000000, $alpha: 0.1);
    background: #f9f9f9;
    padding: 0 0.16rem;
    &__content {
      width: 100%;
      line-height: 0.48rem;
      outline: none;
      border: none;
      background: none;
      font-size: 0.16rem;
      box-sizing: border-box;
      color: $content-notice-fontColor;
      &::placeholder {
        color: $content-notice-fontColor;
      }
    }
  }

  &__register-button {
    background: $btn-bgColor;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
    border-radius: 0.04rem;
    color: $bgColor;
    margin: 0.32rem 0.4rem 0.16rem;
    line-height: 0.48rem;
    font-size: 0.16rem;
    text-align: center;
  }
  &__register-link {
    text-align: center;
    font-size: 0.14rem;
    color: $content-notice-fontColor;
    letter-spacing: 0;
  }
}
</style>
